/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.data.formatter.Formatter;
import fever.data.formatter.NumberBaseRoundType;
import fever.utils.StringUtil;

/**
 * {@code NumberFormatter } class formats a valid number adjusting the decimal 
 * rounding, precision and the thousands separator character.
 * 
 * @author Romain Ecarnot
 */
class fever.data.formatter.NumberFormatter extends Formatter
{
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Decimal separator character to use when outputting formatted decimal numbers. */
	public var decimalSeparator : String;
	
	/** Number of decimal places to include in the output String. */
	public var precision : Number;
	
	/** Specifies how to round the number. */
	public var rounding : String;
	
	/** Character to use as the thousands separator in the output String. */
	public var thousandsSeparator : String;
	
	/** If {@code true}, split the number into thousands increments by using a separator character. */
	public var useThousandsSeparator : Boolean;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructs new {@code NumberFormatter} instance.
	 */
	public function NumberFormatter()
	{
		decimalSeparator = ".";
		precision = -1;
		rounding = NumberBaseRoundType.NONE;
		thousandsSeparator = " ";
		useThousandsSeparator = true;
	}
	
	/**
	 * Formats and returns passed-in {@code value}.
	 * 
	 * @return Formatted {@code String}
	 */
	public function format( value : Number ) : String
	{
		if( isNaN(value) )
		{
			error = 'Unable to format undefined or null Number';
			return '';
		}
		
		var integer : String = '';
		var decimal : String = '';
		var result : String = '';
		var tempDecimalSeparator : String = decimalSeparator;
		
		if( rounding != NumberBaseRoundType.NONE )
		{
			switch ( rounding )
			{
				case NumberBaseRoundType.DOWN : 
					value = Math.floor( value );
					break;
				case NumberBaseRoundType.UP :
					value = Math.ceil( value ); 
				case NumberBaseRoundType.NEAREST :
					value = Math.round( value );
					break;
				default : 
					error = 'Invalid rounding type';
					return '';
			}
			integer = value.toString();
			tempDecimalSeparator = '';
			decimal = '';
		}
		else
		{
			var s : String = value.toString();
			if( s.indexOf(".") )
			{
				var part : Array = s.split(".");
				integer = part[0];
				decimal = part[1];
				
				if( precision > 0 )
					decimal = decimal.substr(0, precision);	
			}
		}
		
		if( useThousandsSeparator )
		{
			var a : Array = new Array();
			var sp : Array = integer.split("");
			var i : Number = sp.length;
			var j : Number = -1;
			
			while( --i >= 0 )
			{
				if( ! ( ++j % 3) )
				{
					if( j != 0 ) a.push( thousandsSeparator );
					a.push( sp[i] );
				}	
				else a.push( sp[i] );		
			}
			
			a.reverse();
			integer = StringUtil.trim( a.join("") );
		}
		
		result = integer + tempDecimalSeparator + decimal;
		
		return result;
	}
	
}